package com.diandian.backend.store.service;

import com.diandian.backend.store.domain.StoreElemeRetailStoreInfoTree;
import com.diandian.backend.store.dto.*;
import com.diandian.backend.store.mapper.StoreElemeRetailStoreInfoTreeMapper;
import com.diandian.backend.store.resp.StoreElemeRetailStoreInfoTreeDetailResp;
import com.diandian.backend.store.resp.StoreElemeRetailStoreInfoTreeListResp;
import com.diandian.backend.store.resp.StoreElemeRetailStoreInfoTreePageResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StoreElemeRetailStoreInfoTreeService {

    @Autowired
    private StoreElemeRetailStoreInfoTreeMapper storeElemeRetailStoreInfoTreeMapper;

    public PageResult page(StoreElemeRetailStoreInfoTreePageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = storeElemeRetailStoreInfoTreeMapper.getCount(requestDTO);

        // 查询数据集合
        List<StoreElemeRetailStoreInfoTree> dataList = storeElemeRetailStoreInfoTreeMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<StoreElemeRetailStoreInfoTreePageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                StoreElemeRetailStoreInfoTreePageResp item = new StoreElemeRetailStoreInfoTreePageResp();
                item.setId(x.getId());
                item.setNodeCode(x.getNodeCode());
                item.setStoreCode(x.getStoreCode());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<StoreElemeRetailStoreInfoTreeListResp> getList(StoreElemeRetailStoreInfoTreeListParamDTO requestDTO){

        // 查询数据集合
        List<StoreElemeRetailStoreInfoTree> dataList = storeElemeRetailStoreInfoTreeMapper.getList(requestDTO);

        // 组装数据
        List<StoreElemeRetailStoreInfoTreeListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                StoreElemeRetailStoreInfoTreeListResp item = new StoreElemeRetailStoreInfoTreeListResp();
                item.setId(x.getId());
                item.setNodeCode(x.getNodeCode());
                item.setStoreCode(x.getStoreCode());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return respList;
    }

    public StoreElemeRetailStoreInfoTreeDetailResp getDetail(StoreElemeRetailStoreInfoTreeDetailParamDTO param) {

        StoreElemeRetailStoreInfoTree dbData = storeElemeRetailStoreInfoTreeMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        StoreElemeRetailStoreInfoTreeDetailResp result = new StoreElemeRetailStoreInfoTreeDetailResp();
        result.setId(dbData.getId());
        result.setNodeCode(dbData.getNodeCode());
        result.setStoreCode(dbData.getStoreCode());
        result.setRemark(dbData.getRemark());
        result.setBelong(dbData.getBelong());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUserName(dbData.getCreateUserName());
        result.setCreateTime(dbData.getCreateTime());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    public StoreElemeRetailStoreInfoTreeDetailResp getNodeCodeByStoreCode(String belong, String orgId, String orgCode, String storeCode) {


        StoreElemeRetailStoreInfoTreeDetailParamDTO param = new StoreElemeRetailStoreInfoTreeDetailParamDTO();
        param.setStoreCode(storeCode);
        param.setBelong(belong);
        param.setOrgCode(orgCode);
        param.setOrgId(orgId);
        StoreElemeRetailStoreInfoTree dbData = storeElemeRetailStoreInfoTreeMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        StoreElemeRetailStoreInfoTreeDetailResp result = new StoreElemeRetailStoreInfoTreeDetailResp();
        result.setId(dbData.getId());
        result.setNodeCode(dbData.getNodeCode());
        result.setStoreCode(dbData.getStoreCode());
        result.setRemark(dbData.getRemark());
        result.setBelong(dbData.getBelong());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUserName(dbData.getCreateUserName());
        result.setCreateTime(dbData.getCreateTime());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    public void updateById(StoreElemeRetailStoreInfoTreeUpdateByIdParamDTO param) {
        param.check();

        StoreElemeRetailStoreInfoTreeDetailParamDTO queryDetailDTO = new StoreElemeRetailStoreInfoTreeDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        StoreElemeRetailStoreInfoTree dbData = storeElemeRetailStoreInfoTreeMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        storeElemeRetailStoreInfoTreeMapper.updateById(param);
    }

    public void updateRelation(StoreElemeRetailStoreInfoTreeUpdateByIdParamDTO param) {
        param.check();
        storeElemeRetailStoreInfoTreeMapper.updateRelation(param);
    }

    public void deleteById(StoreElemeRetailStoreInfoTreeDeleteByIdParamDTO paramDTO) {
        paramDTO.check();
        storeElemeRetailStoreInfoTreeMapper.deleteById(paramDTO);
    }

    public void deleteByStoreCode(String belong, String orgId, String orgCode, String storeCode) {
        storeElemeRetailStoreInfoTreeMapper.deleteByStoreCode(belong, orgId, orgCode, storeCode);
    }

    public void remove(String belong, String orgId, String orgCode, String nodeCode, String storeCode) {
        storeElemeRetailStoreInfoTreeMapper.remove(belong, orgId, orgCode, nodeCode, storeCode);
    }

    public void add(StoreElemeRetailStoreInfoTreeAddParamDTO param) {
        param.setCreateTime(new Date());
        param.setDel(0);
        storeElemeRetailStoreInfoTreeMapper.add(param);
    }
}
